<?php

namespace Biz\model\Album;

use Doraemon\pockets\datebase\BaseDbModel;
use Doraemon\tools\HhzEnv;

class AlbumListModel extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_store';

    const _CONFIG_SELECT_ = 'db_store';

    const _DATABASE_ = 'store';

    public const _TABLE_NAME = 'album_list';

    const STATUS_UNTREATED = 0; // 待审核
    const STATUS_SUCCESS = 1; // 已发布
    const STATUS_WAIT_CHECK = 2; // 待复审
    const STATUS_DRAFT = 3; // 状态为待发布 定时发布 时间未到的时候 为待发布
    const STATUS_USER_PRIVATE = 7; // 用户设置私密状态
    const STATUS_DELETE = 9; // 状态为删除
    const STATUS_PRIVATE = 11; // 后台设置为仅自己可见

    const BASE_VISIBLE_STATUS = [
        self::STATUS_UNTREATED,
        self::STATUS_SUCCESS,
        self::STATUS_WAIT_CHECK,
        self::STATUS_DRAFT,
        self::STATUS_USER_PRIVATE,
        self::STATUS_PRIVATE,
    ];

    //公开清单 状态
    const STATS_PUBLIC_SHOW_MAP = [
        self::STATUS_UNTREATED,
        self::STATUS_SUCCESS,
        self::STATUS_WAIT_CHECK,
    ];

    //列表展示公开状态
    const STATS_LIST_PUBLIC_SHOW_MAP = [
        self::STATUS_SUCCESS,
        self::STATUS_WAIT_CHECK,
    ];

    const STATUS_MAP = [
        self::STATUS_UNTREATED => '待审核',
        self::STATUS_SUCCESS => '已审核',
        self::STATUS_WAIT_CHECK => "待复审",
        self::STATUS_DRAFT => '待发布',
        self::STATUS_USER_PRIVATE => '用户设置私密状态',
        self::STATUS_PRIVATE => '后台设置为仅自己可见',
    ];

    const ADM_STATUS_MAP = [
        self::STATUS_SUCCESS => '已审核',
        self::STATUS_PRIVATE => '后台设置为仅自己可见',
        self::STATUS_USER_PRIVATE => '用户设置私密状态',
        self::STATUS_WAIT_CHECK => "待复审",
        self::STATUS_UNTREATED => '待审核',
    ];
    const SHOW_STATUS_LIST = [
        self::STATUS_NORMAL,
        self::STATUS_UNTREATED,
        self::STATUS_WAIT_CHECK,
        self::STATUS_USER_PRIVATE,
        self::STATUS_PRIVATE,
    ];

    const QUALITY_COMMON = 1;//普通
    const QUALITY_BAD = 2;//差
    const QUALITY_GOOD = 3;//优秀
    const QUALITY_EXCELLENT = 4;//精选

    const SCORE_COMMON = 60;//普通
    const SCORE_BAD = 5;//差
    const SCORE_GOOD = 70;//优秀
    const SCORE_EXCELLENT = 80;//精选

    const SCORE_MAP = [
        self::SCORE_COMMON => '普通',
        self::SCORE_BAD => '差',
        self::SCORE_GOOD => "优秀",
        self::SCORE_EXCELLENT => '精选'

    ];

    const QUALITY_MAP = [
        self::QUALITY_COMMON => '普通',
        self::QUALITY_BAD => '差',
        self::QUALITY_GOOD => '优秀',
        self::QUALITY_EXCELLENT => '精选',
    ];


    const SCORE_QUALITY_MAP = [
        0 => 0,
        self::SCORE_COMMON => self::QUALITY_COMMON,
        self::SCORE_BAD => self::QUALITY_BAD,
        self::SCORE_GOOD => self::QUALITY_GOOD,
        self::SCORE_EXCELLENT => self::QUALITY_EXCELLENT
    ];

    const TAG_APPLICATION_NAME = '好物清单'; //标签包用途名称
    const TAG_MAPPING_NAME = [
        '商品品类',
        '大分类',
    ];

    const DEFAULT_UID_TEST = 19627502; //默认uid 测试环境 测试使用
    const DEFAULT_UID_GRAY = 15071988; //默认uid gray 测试使用
    const DEFAULT_UID_PRODUCT = 11941; //默认uid 生产环境 好好住官号uid

    const DEFAULT_UID_HAOHAOZHU = [
        11941, 6084737, 15393722, 16088374
    ];
    const OFFICIAL_UID_MAP = [
        11941 => '好好住',
        6084737 => '好好住编辑部',
        15393722 => '好物发现官',
        16088374 => '咫尺小程序',
    ];

    const ALBUM_WIKI_LIMIT = 50; //清单最大支持商品数

    public static function insertData($data)
    {
        if (!isset($data['addtime'])) {
            $data['addtime'] = date('Y-m-d H:i:s');
        }
        if (!isset($data['edittime'])) {
            $data['edittime'] = date('Y-m-d H:i:s');
        }
        $ret = self::insert($data);
        if ($ret->errorCode() !== '00000') {
            return false;
        }

        return self::id();
    }

    public static function updateDataByWhere($data, $where)
    {
        if (!isset($data['edittime'])) {
            $data['edittime'] = date('Y-m-d H:i:s');
        }
        $ret = self::update($data, $where);
        if ($ret->errorCode() !== '00000') {
            return false;
        }

        return true;
    }

    public static function selectByWhere($where, $field = '*')
    {
        $result = [];
        $list = self::select($field, $where);
        if ((is_array($field) || $field == '*') && !empty($list)) {
            foreach ($list as $row) {
                $info = self::formatInfo($row);
                $result[] = $info;
            }
        }

        return $result;
    }

    public static function formatInfo($info)
    {
        foreach ($info as $key => $val) {
            switch ($key) {
                case 'cover_img':
                    $info['cover_img_url'] = $val;
                    break;
                case 'status':
                    $info['status_name'] = self::STATUS_MAP[$val];
                    break;
                case 'publish_time':
                    if ($val) {
                        $info['publish_time_string'] = date('Y-m-d H:i', $val);
                    } else {
                        $info['publish_time_string'] = '';
                    }
                    break;
                default;
            }

        }

        return $info;
    }


    /**
     * 添加评论、点赞、收藏、用于查询清单数据使用
     * @param $album_id
     * @return array|mixed
     */
    public static function getAlbumById($album_id)
    {
        return self::get("*", [
            'album_id' => $album_id,
            'status' => self::BASE_VISIBLE_STATUS,
        ]);
    }

    /**
     * @param $album_id
     * @return array|mixed
     */
    public static function getDelAlbumById($album_id)
    {
        return self::get("*", [
            'album_id' => $album_id,
            'status' => [AlbumListModel::STATUS_DELETE, AlbumListModel::STATUS_PRIVATE],
        ]);
    }

    public static function getByWhere($where, $field = '*')
    {
        $data = self::get($field, $where);
        if (!empty($data) && (is_array($field) || $field == '*')) {
            $data = self::formatInfo($data);
        }

        return $data;
    }

    /**
     * 获取不同环境下清单创建者的默认uid
     * @return int
     */
    public static function getDefaultUid(): int
    {
        return self::DEFAULT_UID_PRODUCT;
        if (HhzEnv::isProduction()) {
            return self::DEFAULT_UID_PRODUCT;
        }
        if (HhzEnv::isGray()) {
            return self::DEFAULT_UID_GRAY;
        }

        return self::DEFAULT_UID_TEST;


    }

    /**
     * 根据用户uid删除内容
     *
     * @param $uid
     * @return int
     * @time 2022/11/17 16:01
     * @desc
     */
    public static function delByUid($uid)
    {
        return self::update(['status' => self::STATUS_DELETE], ['uid' => $uid])->rowCount();
    }

    public static function getPublishCountByUid($uid)
    {
        return self::count([
            'uid' => $uid,
            'status' => [self::STATUS_UNTREATED, self::STATUS_WAIT_CHECK, self::STATUS_SUCCESS, self::STATUS_PRIVATE, self::STATUS_USER_PRIVATE]
        ]);
    }


}
